package com.situ.mall.controller;

import com.situ.mall.pojo.Admin;
import com.situ.mall.pojo.query.AdminQuery;
import com.situ.mall.service.IAdminService;
import com.situ.mall.util.PageResult;
import com.situ.mall.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @RequestMapping("/selectAll")
    public List<Admin> selectAll(){
        System.out.println("AdminController.selectAll");
        List<Admin> list = adminService.selectAll();
        return list;
    }

    @RequestMapping("/selectByPage")
    public PageResult<Admin> selectByPage(AdminQuery adminQuery){
        System.out.println("AdminController.selectByPage");
        PageResult<Admin> pageResult = adminService.selectByPage(adminQuery);
        return pageResult;
    }

    //@MyLog(module = "管理员模块")
    @RequestMapping("/add")
    public Result add(Admin admin) {
        System.out.println("AdminController.add");
        adminService.add(admin);
        return Result.ok("添加成功");
    }
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        adminService.deleteById(id);
        return Result.ok("删除成功");
    }
    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids){
        adminService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/login")
    public Result login(String name, String password, String code, HttpSession session) {
        System.out.println("AdminController.login");
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        Admin admin = adminService.login(name, password);
        if (admin == null) {
            return Result.error("用户名或密码错误");
        }

        session.setAttribute("admin", admin);
        return Result.ok("登录成功");
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("admin");
        return Result.ok("退出成功");
    }
    @RequestMapping("/selectById")
    public Result selectById(Integer id){
        Admin admin = adminService.selectById(id);
        return Result.ok(admin);
    }
    @RequestMapping("/update")
    public Result update(Admin admin){
        adminService.update(admin);
        return Result.ok("更新成功");
    }
}
